package ch8;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.*;

public class ObjectSendThread extends Thread {
	private Socket s;

	public ObjectSendThread(Socket s) {
		this.s = s;
	}

	private Map<String, Object> map = new HashMap<>();
	{
		map.put("clsName", "java61");
		map.put("num", "29");
		map.put("addr", "304");
	}

	@Override
	public void run() {
		map.put("now", new Date());
		try {
			OutputStream out = s.getOutputStream();
			InputStream in = s.getInputStream();

			ObjectOutputStream oin = new ObjectOutputStream(out);
			ObjectInputStream oo = new ObjectInputStream(in);

			oin.writeObject(map);
			oin.flush();
			String str = (String) oo.readObject();
			System.out.println("收到了" + s + "的信息：" + str);
			oin.close();
			oo.close();
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			if (s != null)
				try {
					s.close();
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
		}

	}
}
